Programming Languages Section

Crystal Programming Language

What is Crystal Programming Language
Post by Amina Delali, March 21th, 2023

Some Facts

Crystal is a free and open source programming language inspired by Ruby. It is a general purpose programming language, compiled, and object oriented. It implements static type checking as well as type inference. I also implements concurrency through threads called fibers.

An other interesting fact that you may want to know about the language is that if you want to add a library to your project, all you have to do is to specify its Git repository in a Yaml file, and it will be automatically fetched.

Several companies already used Crystal for different types of projects. Like Invidious in its alternative front-end to YouTube project. Or BlockVue that provides interactive leasing and tour technology platform via VR.



How to install it

  • on Windows:

    To install Crystal on Windows you will need a command line installer called Scoop. After you install it you will use it to install Crystal

    • Install Scoop: Open PowerShell Terminal and run the following Command: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
      irm get.scoop.sh | iex
      After the installation, you add: "C:\Users\\scoop\shims" the to the path environment variable. For example for me, I added the following path: C:\Users\AMINA\scoop\shims.
    • Install Git: You will first use Scoop to install Git and add the crystal repository. To do that, just open the terminal, and run the following commands: scoop install git
      scoop bucket add crystal-preview https://github.com/neatorobito/scoop-crystal
    • Install Visual Studio Build Tools: The official installation page suggests to run the following command if you don't have the x64 Native Tools Command Prompt available: scoop install vs_2022_cpp_build_tools The command didn't work for me. So, I had to install Visual Studio C++ build tools manually:
      • Downloaded the installer from this page
      • Run the installer, then I clicked on install again (to install Visual studio build tools)
      • After that I selected Desktop development with c++ and clicked on install again
      • Added the following path: C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin to the path environment variable
      • Restarted the system.

      You need the build tools to be able ro run, compile, and build crystal code. Without that, the hello world code that I will describe later will not work.

      To learn more about this installation, you can follow the instructions from this page.

    • Install Crystal: Now you can install Crystal by running the following command: scoop install crystal To check the installation run the following command: crystal --version
    For more details about the installation, you can check the official installation page.
  • On Ubuntu :

      To install Crystal on Ubuntu you will have to run the commands corresponding to your distribution. For example, if you want to install it for Ubuntu22.10 run the following commands: echo 'deb http://download.opensuse.org/repositories/devel:/languages:/crystal/xUbuntu_22.10/ /' | sudo tee /etc/apt/sources.list.d/devel:languages:crystal.list

      curl -fsSL https://download.opensuse.org/repositories/devel:languages:crystal/xUbuntu_22.10/Release.key | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/devel_languages_crystal.gpg > /dev/null

      sudo apt update
      sudo apt install crystal
      To check the installation run the following command: crystal --version

      The installation commands for the other distributions can be found in this installation page.

The Hello World Example

Create a file named hello.cr. In that file, write the following code:

puts "Hello World!"

Open the terminal, and run the following command to compile and run your code:
crystal run hello.cr If you want to generate an executable (a binary file with the same name ), simply run the following command: crystal build hello.cr



Additional Information

For more information about the language and the corresponding code, you can check the following pages:

Something to say ?

If you want to add something about the language or about this post, please feel free to do it by commenting below 🙂 .